package com.lonely.fx;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.ColorPicker;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 * @date: 2022-07-18 14:38
 * @author: ztkj-hzb
 * @description: 取色器组件设置
 **/
public class ColorPickerSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        HBox hBox = new HBox();

        //TODO 创建取色器对象，默认显示为白色
        ColorPicker colorPicker = new ColorPicker();

        //指定显示颜色得取色器
        //ColorPicker colorPicker = new ColorPicker(Color.PINK);

        //设置颜色
        colorPicker.setValue(Color.PINK);

        //选择颜色或者颜色值变化事件
        colorPicker.valueProperty().addListener((observable, oldValue, newValue) -> {
            System.out.println(newValue.toString());

            //让父容器得颜色设置为选择的颜色,例如：0xa97e3fff 前两位没用，中间六位为颜色，最后两位为透明度
            String colorValue = newValue.toString();
            hBox.setStyle(String.format("-fx-background-color: #%s",colorValue.substring(2,6)));
        });

        hBox.getChildren().addAll(colorPicker);
        primaryStage.setScene(new Scene(hBox));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();
    }


}
